﻿Imports System.Data.OleDb
Imports DTO

Public Class TheLoaiDAO
    Public Function LayDanhSach() As List(Of TheLoaiDTO)
        Dim ds As New List(Of TheLoaiDTO)
        Dim cn As OleDbConnection
        Dim strSQL As String
        'B1:tao chuoi ket noi va mo ket noi bang doi tuong ket noi
        cn = DatabaseProvider.connectData()
        'tao chuoi strSQL thao tac CSDL
        strSQL = "Select * From TheLoai"
        'thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        Dim tl As TheLoaiDTO
        While (dr.Read())
            tl = New TheLoaiDTO()
            'tl.MaTheLoai = dr("MaTheLoai")
            'tl.TenTheLoai = dr("TenTheLoai")
            tl.MaTheLoai = dr.GetInt32(0)
            tl.TenTheLoai = dr.GetString(1)
            ds.Add(tl)

        End While
        cn.Close()
        Return ds

    End Function

    Public Function TimKiem(ByVal maTheloai As Integer) As TheLoaiDTO
        Dim tlDTO As New TheLoaiDTO
        'tao chuoi ket noi,mo ket noi bang doi tuong ket noi
        Dim cn As OleDbConnection
        cn = DatabaseProvider.connectData()
        Dim strSQL As String
        strSQL = " Select * From TheLoai Where MaTheLoai = ?"
        'thuc thi chuoi strSQL
        Dim cmd As New OleDbCommand(strSQL, cn)
        cmd.Parameters.Add("@Ma", OleDbType.Integer)
        cmd.Parameters("@Ma").Value = maTheloai
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        While (dr.Read())
            tlDTO.MaTheLoai = dr("MaTheLoai")
            tlDTO.TenTheLoai = dr("TenTheLoai")
        End While
        'dong ket noi CSDL
        dr.Close()
        cn.Close()
        Return tlDTO

    End Function


End Class
